/*
 * Borrowed from Nginx, Copyright (C) Igor Sysoev
 */
#ifndef __PLATFORM_LIB_PALLOC_H__
#define __PLATFORM_LIB_PALLOC_H__
#ifdef __cplusplus
extern "C"
{
#endif

#include "pf_common.h"

/******************************************************************************************************/

typedef struct pf_pool_cleanup pf_pool_cleanup_t;

typedef struct pf_pool pf_pool_t;

/*********************************************************************************************
Function Name:  pf_pool_create
Description  :  pool constructor. Would pre-alloc one block for pool initialization.
Inputs       :  size_t size                         : Size of pool block.
Outputs      :  return value                        : Pool pointer.
                                                      Caller must call pf_pool_destroy() to destroy it.
                                                    : NULL. Pool create fail.
ErrorCodes   :  no
History      :
---------------------------------------------------------------------------------------------
Author                 Date                         Comments
lijz                   2011-6-24                    create
**********************************************************************************************/
extern pf_pool_t* pf_pool_create(size_t size);

/*********************************************************************************************
Function Name:  pf_pool_destroy
Description  :  Pool destructor.
Inputs       :  pf_pool_t* pool   :  Pool pointer to destroy. Must be NOT NULL.
Outputs      :  no
ErrorCodes   :  no
History      :
---------------------------------------------------------------------------------------------
Author                 Date                         Comments
lijz                   2011-6-24                    create
**********************************************************************************************/
extern void pf_pool_destroy(pf_pool_t* pool);

/*********************************************************************************************
Function Name:  pf_pool_reset
Description  :  Reset the pool to have only one block.
Inputs       :  pf_pool_t* pool   :  Pool pointer to reset. Must be NOT NULL.
Outputs      :  no
ErrorCodes   :  no
History      :
---------------------------------------------------------------------------------------------
Author                 Date                         Comments
lijz                   2011-6-24                    create
**********************************************************************************************/
extern void pf_pool_reset(pf_pool_t* pool);

/*********************************************************************************************
Function Name:  pf_palloc
Description  :  Alloc buffer from pool. Buffer pointer would be alignment of sizeof( unsigned long ).
Inputs       :  pf_pool_t* pool   :  Pool pointer. Must be NOT NULL.
                size_t size       :  Buffer size to alloc from pool. Must be more than zero.
Outputs      :  return value      :  (void *)buffer pointer.
                                     NULL. Alloc buffer fail.
ErrorCodes   :  no
History      :
---------------------------------------------------------------------------------------------
Author                 Date                         Comments
lijz                   2011-6-24                    create
**********************************************************************************************/
extern void* pf_palloc(pf_pool_t* pool, size_t size);

/*********************************************************************************************
Function Name:  pf_pnalloc
Description  :  Alloc buffer from pool.
Inputs       :  pf_pool_t* pool   :  Pool pointer. Must be NOT NULL.
                size_t size       :  Buffer size to alloc from pool. Must be more than zero.
Outputs      :  return value      :  (void *)buffer pointer.
                                     NULL. Alloc buffer fail.
ErrorCodes   :  no
History      :
---------------------------------------------------------------------------------------------
Author                 Date                         Comments
lijz                   2011-6-24                    create
**********************************************************************************************/
extern void* pf_pnalloc(pf_pool_t* pool, size_t size);

/*********************************************************************************************
Function Name:  pf_pcalloc
Description  :  Alloc buffer from pool. Alloced buffer has memset() to zero.
Inputs       :  pf_pool_t* pool   :  Pool pointer. Must be NOT NULL.
                size_t size       :  Buffer size to alloc from pool. Must be more than zero.
Outputs      :  return value      :  (void *)buffer pointer.
                                     NULL. Alloc buffer fail.
ErrorCodes   :  no
History      :
---------------------------------------------------------------------------------------------
Author                 Date                         Comments
lijz                   2011-6-24                    create
**********************************************************************************************/
extern void* pf_pcalloc(pf_pool_t* pool, size_t size);

/*********************************************************************************************
Function Name:  pf_pmemalign
Description  :  (Reserved)
Inputs       :


Outputs      :  return value      :

ErrorCodes   :  no
History      :
---------------------------------------------------------------------------------------------
Author                 Date                         Comments
lijz                   2011-6-24                    create
**********************************************************************************************/
extern void* pf_pmemalign(pf_pool_t* pool, size_t size, size_t alignment);

/*********************************************************************************************
Function Name:  pf_pfree
Description  :  Reback buffer to pool.
Inputs       :  pf_pool_t* pool   :  Pool pointer. Must be NOT NULL.
                void* p           :  Buffer pointer. Must be NOT NULL.
Outputs      :  return value      :  0. Always return 0.

ErrorCodes   :  no
History      :
---------------------------------------------------------------------------------------------
Author                 Date                         Comments
lijz                   2011-6-24                    create
**********************************************************************************************/
extern int32_t pf_pfree(pf_pool_t* pool, void *p);

/*********************************************************************************************
Function Name:  pf_pool_cleanup_add
Description  :
Inputs       :
Outputs      :
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                         Comments
lijz                   2011-6-24                    create
**********************************************************************************************/
extern pf_pool_cleanup_t* pf_pool_cleanup_add(pf_pool_t* p, size_t size);

/******************************************************************************************************/

#ifdef __cplusplus
}
#endif
#endif /*#ifndef __PLATFORM_LIB_PALLOC_H__ */

